
package com.rt.schedulenew.web;

import com.rt.schedulebase.dto.DeptMailboxDto;
import com.rt.schedulebase.dto.DeptNoticeDto;
import com.rt.schedulebase.dto.DictUsersDto;
import com.rt.schedulebase.dto.ExamApplyDto;
import com.rt.schedulenew.api.ICommonService;
import com.rt.schedulenew.api.IDeptMailboxService;
import com.rt.schedulenew.api.IDeptNoticeService;
import com.rt.schedulenew.api.IDictUsersService;
import com.rt.schedulenew.service.impl.CommonServiceImpl;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.checkparam.config.Check;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping({ "/deptNotice" })
public class DeptNoticeController
{
    private static  Logger log = LoggerFactory.getLogger(DeptNoticeController.class);
    @Autowired
    IDeptNoticeService deptNoticeService;
    @Autowired
    IDeptMailboxService deptMailboxService;
    @Autowired
    IDictUsersService usersService;
    @Autowired
    ICommonService commonService;
    
    @PostMapping({ "/list" })
    @Check({ "userId" })
    public JsonResult getDeptNotices(@RequestBody  DeptNoticeDto dto) {
        return this.deptNoticeService.getDeptNoticeList(dto);
    }
    
    @PostMapping({ "/mailbox/update" })
    @Check({ "noticeId" })
    public JsonResult editNotice(@RequestBody  DeptMailboxDto dto) {
         String userid = dto.getUserId();
        if (StringUtils.isNotBlank(dto.getProcessFlag())) {
            dto.setUserId((String)null);
        }
        if (!this.deptMailboxService.update(dto)) {
            return JsonResultUtil.failure("修改失败");
        }
        if (StringUtils.isNotBlank(dto.getProcessFlag())) {
             DictUsersDto users = this.usersService.getUserInfo(userid);
             ExamApplyDto ea = new ExamApplyDto();
            ea.setApplyNo(dto.getApplyNo());
            ea.setActType(ConstantsUtil.ActType.NOTICE_AFFIRM.getType());
            this.commonService.addExamScheduleAct(ea, users);
        }
        return JsonResultUtil.success("操作成功");
    }
    
    @PostMapping({ "/info" })
    @Check({ "noticeId" })
    public JsonResult getDeptNoticeInfo(@RequestBody  DeptNoticeDto dto) {
         JsonResult result = this.deptNoticeService.getDeptNoticeList(dto);
         List<DeptNoticeDto> list = (List<DeptNoticeDto>)result.getResult();
        return JsonResultUtil.success("成功", list.get(0));
    }
    
    @PostMapping({ "/mailbox/unreadCount" })
    @Check({ "userId" })
    public JsonResult getDeptNoticeUnreadCount(@RequestBody  DeptMailboxDto dto) {
         int unreadCount = this.deptMailboxService.unreadCount(dto);
         Map<String, Object> map = new HashMap<String, Object>();
        map.put("unreadCount", unreadCount);
        return JsonResultUtil.success("成功", map);
    }
}
